package rocket.logs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author justin
 * @version
 *
 */
public class Write {
	/**
	 * based on the type of log provided, the method switches to the correct .log file
	 * and writes to the end of the log.
	 * @param w
	 * @param s
	 */
	public Write(WriteType w, String s){
		switch (w){
		case APPOINTMENT:	//Appointment creation and modification
			writeOut(new File("appt.log"), s);
			break;
		case AUTH:			//user logins, auth checks, bank info, SS#'s
			writeOut(new File("auth.log"), s);
			break;
		case USER:			//new users and changes made to users
			writeOut(new File("users.log"), s);
			break;
		case SERVER:		//server startup, errors, exceptions
			writeOut(new File("server.log"), s);
			break;
			default:
				String output = "Bad Write Type: " + w.toString();
				System.out.println(output);
				writeOut(new File("server.log"), output);
				break;
		}
	}

	/**
	 * @param f
	 * @param s
	 */
	public void writeOut(File f, String s){
		try {
			boolean append = true;
			BufferedWriter out = new BufferedWriter(new FileWriter(f, append));
			String dateFormat = "yyyy:MM:dd::HH:mm:ss";
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
			
			String time = sdf.format(cal.getTime());
			String logEntry = "[" + time + "] - " + s;
			
			out.write(logEntry);
			out.newLine();
			out.flush();
			out.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String toString(){
		return "write";
	}
}
